package com.git.smp.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.git.smp.core.PageRequest;
import com.git.smp.core.ResponseMessage;
import com.git.smp.dao.SmpPermissionMapper;
import com.git.smp.entity.SmpPermission;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@RestController
@RequestMapping("/permission")
public class SmpPermissionController {
	
	Logger logger = LoggerFactory.getLogger(SmpPermissionController.class);
	
	@Autowired
	SmpPermissionMapper smpPermissionMapper;
	
	@Autowired
	private HttpServletRequest request;
	
	@RequestMapping("/getPermissionListByUserName")
	public ResponseMessage<List<SmpPermission>> getPermissionListByUserName(){
		ResponseMessage<List<SmpPermission>> responseMessage = new ResponseMessage<List<SmpPermission>>();
		responseMessage.setCode("0000");
		responseMessage.setMessage("查询权限信息成功");
		
		String userName = request.getHeader("userName");
		List<SmpPermission> smpPermissionList =  smpPermissionMapper.getPermissionListByUserName(userName);
		responseMessage.setExtend(smpPermissionList);
		
		return responseMessage;
		
	}
	
	
	//分页查询权限表信息
	@RequestMapping("/getPermissionListByPage")
	public ResponseMessage<PageInfo<SmpPermission>> getPermissionListByPage(
			@RequestBody PageRequest<SmpPermission> pageRequest){
		
		ResponseMessage<PageInfo<SmpPermission>> responseMessage = new ResponseMessage<PageInfo<SmpPermission>>();
		responseMessage.setCode("0000");
		responseMessage.setMessage("分页查询权限表信息成功！");
		
		SmpPermission smpPermission = pageRequest.getBody();
		
		PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
		List<SmpPermission> listSmpPermission = smpPermissionMapper.getPermissionListByPage(smpPermission.getPermissionName());
		
		if(listSmpPermission == null) {
			responseMessage.setCode("0001");
			responseMessage.setMessage("分页查询权限表信息失败！");
		}
		
		PageInfo<SmpPermission> pageInfo = new PageInfo<SmpPermission>(listSmpPermission, pageRequest.getPageSize());
		responseMessage.setExtend(pageInfo);
		logger.info(responseMessage.getMessage());
		return responseMessage;
	}
	
	//获取权限表列表信息
	@RequestMapping("/getPermissionList")
	public ResponseMessage<List<SmpPermission>> getPermissionList(){
		ResponseMessage<List<SmpPermission>> responseMessage = new ResponseMessage<List<SmpPermission>>();
		responseMessage.setCode("0000");
		responseMessage.setMessage("权限表列表信息查询成功！");
		
		List<SmpPermission> listPermission = smpPermissionMapper.getPermissionList();
		
		if(listPermission == null) {
			responseMessage.setCode("0001");
			responseMessage.setMessage("权限表列表信息获取失败！");
		}
		
		responseMessage.setExtend(listPermission);
		logger.info(responseMessage.getMessage());
		return responseMessage;
		
		
	}
	//根据id删除权限信息
	@RequestMapping("delPermissionInfoById")
	public ResponseMessage<?> delPermissionInfoById(@RequestParam String permissionId){
		ResponseMessage<?> responseMessage = new ResponseMessage<>();
		responseMessage.setCode("0000");
		responseMessage.setMessage("权限信息删除成功！");
		
		int code = smpPermissionMapper.deleteByPrimaryKey(permissionId);
		logger.info(responseMessage.getMessage());
		return responseMessage;
	}
	
	//修改权限信息
	@RequestMapping("updatePermissionInfo")
	public ResponseMessage<?> updatePermissionInfo(
			@RequestBody SmpPermission smpPermission){
		ResponseMessage<?> responseMessage = new ResponseMessage<>();
		responseMessage.setCode("0000");
		responseMessage.setMessage("权限信息修改成功！");
		
		int code = smpPermissionMapper.updateByPrimaryKey(smpPermission);
		logger.info(responseMessage.getMessage());
		return responseMessage;
		
	}
	
	//添加权限信息
	@RequestMapping("addPermissionInfo")
	public ResponseMessage<?> addPermissionInfo(
			@RequestBody SmpPermission smpPermission){
		ResponseMessage<?> responseMessage = new ResponseMessage<>();
		responseMessage.setCode("0000");
		responseMessage.setMessage("权限信息添加成功！");
		
		int code = smpPermissionMapper.insert(smpPermission);
		logger.info(responseMessage.getMessage());
		return responseMessage;
		
	}
	

}
